package com.jlwl.houseservice.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Employee;
import com.jlwl.houseservice.service.EmployeeService;
import com.jlwl.houseservice.vo.EmployeeDetailVO;
import com.jlwl.houseservice.vo.EmployeeQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员员工控制器
 */
@RestController
@RequestMapping("/admin/employees")
@PreAuthorize("hasRole('ADMIN')")
public class AdminEmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 获取员工列表
     */
    @GetMapping
    public Result<Page<Employee>> list(EmployeeQueryVO queryVO) {
        Page<Employee> page = employeeService.pageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取员工详情
     */
    @GetMapping("/{id}")
    public Result<EmployeeDetailVO> getDetail(@PathVariable Integer id) {
        EmployeeDetailVO detailVO = employeeService.getEmployeeDetail(id);
        if (detailVO == null) {
            return Result.error("员工不存在");
        }
        return Result.success(detailVO);
    }

    /**
     * 创建员工
     */
    @PostMapping
    public Result<Void> create(@RequestBody Employee employee) {
        boolean success = employeeService.createEmployee(employee);
        if (!success) {
            return Result.error("创建员工失败");
        }
        return Result.success();
    }

    /**
     * 更新员工
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody Employee employee) {
        employee.setId(id);
        boolean success = employeeService.updateEmployee(employee);
        if (!success) {
            return Result.error("更新员工失败");
        }
        return Result.success();
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean success = employeeService.deleteEmployee(id);
        if (!success) {
            return Result.error("删除员工失败");
        }
        return Result.success();
    }

    /**
     * 更新员工状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        boolean success = employeeService.updateEmployeeStatus(id, status);
        if (!success) {
            return Result.error("更新员工状态失败");
        }
        return Result.success();
    }
} 